package com.mydigitallife.timetracker;

import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class customers extends ListActivity {

	private final String MY_DATABASE_NAME = "TimeTrack";

	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  	  
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.track, selectall()));

	  final ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	    	id = id + 1;
	    	startTracking(id);
	    	
			Intent myIntent = new Intent(view.getContext(), timetracker.class);
			startActivityForResult(myIntent, 0);

	    }
	  });
	}


	
	public List<String> selectall(){
		SQLiteDatabase myDB = null;
		  myDB = this.openOrCreateDatabase("TimeTrack", MODE_WORLD_READABLE, null);
		
		List<String> PROJECTS = new ArrayList<String>();
		Cursor cursor = myDB.query("projects", new String[]{"projectname"}, null, null, null, null, null);
		if(cursor.moveToFirst()){
			do{
				PROJECTS.add(cursor.getString(0));
			} while(cursor.moveToNext());
		}
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		return PROJECTS;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.menu, menu);
		
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		case R.id.addProject:
			popupaddproject();
			return true;
		}
		return false;
	}
	
	
	public SQLiteDatabase connectDB(){
		SQLiteDatabase myDB = null;
		myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
		return myDB;
	}
	
	private void addProjectdebug(){
		connectDB().execSQL("INSERT INTO projects VALUES(1,'test')");
		connectDB().close();
	}
	
	private void addProject(String name){
		connectDB().execSQL("INSERT INTO projects (projectname) VALUES('"+ name +"')");
		setListAdapter(new ArrayAdapter<String>(this, R.layout.track, selectall()));
		connectDB().close();
	}
	
	private void popupaddproject(){
		final FrameLayout f1 = new FrameLayout(this);
		final EditText input = new EditText(this);
		input.setGravity(Gravity.CENTER);
		
		f1.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
		
		new AlertDialog.Builder(this)
			.setView(f1)
			.setTitle("Project name")
			.setPositiveButton("Add project", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					addProject(input.getText().toString());
					dialog.dismiss();
				}
			})
			.setNegativeButton("Cancel",  new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					
				}
			}).create().show();
		
		new AlertDialog.Builder(this).setView(f1);
	}
	
	private void startTracking(long projectId){
		connectDB().execSQL("INSERT into tracking (_id, starttime) VALUES('" + projectId + "', datetime())");
		connectDB().close();
	}
}
